package com.bdc.whatwhat.authentification;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

import com.bdc.whatwhat.R;
import com.sromku.simple.fb.Permission;
import com.sromku.simple.fb.SimpleFacebook;
import com.sromku.simple.fb.SimpleFacebookConfiguration;
import com.sromku.simple.fb.entities.Profile;
import com.sromku.simple.fb.listeners.OnInviteListener;
import com.sromku.simple.fb.listeners.OnLoginListener;
import com.sromku.simple.fb.listeners.OnProfileListener;

import java.util.List;

/**
 * Created by florentchampigny on 24/06/14.
 */
public class Facebook {

    public interface FacebookDelegate {
        public void onFacebookConnected(String email, String id, String session, String nom, String prenom);
    }

    public static final String NAMESPACE = "com.bdc.whatwhat";

    SimpleFacebook mSimpleFacebook;
    Permission[] permissions = new Permission[]{
            Permission.USER_PHOTOS,
            Permission.EMAIL
    };

    FacebookDelegate delegate;
    Activity activity;

    public Facebook(Activity activity, FacebookDelegate delegate) {
        this.delegate = delegate;
        this.activity = activity;

        SimpleFacebookConfiguration configuration = new SimpleFacebookConfiguration.Builder()
                .setAppId(activity.getResources().getString(R.string.app_id))
                .setNamespace(NAMESPACE)
                .setPermissions(permissions)
                .build();
        SimpleFacebook.setConfiguration(configuration);
    }

    public void onResume() {
        mSimpleFacebook = SimpleFacebook.getInstance(activity);
    }


    OnLoginListener onLoginListener = new OnLoginListener() {
        @Override
        public void onLogin() {
            // change the state of the button or do whatever you want
            Log.i("FB", "Logged in");
            mSimpleFacebook.getProfile(new OnProfileListener() {
                @Override
                public void onComplete(Profile response) {
                    super.onComplete(response);
                    String session = mSimpleFacebook.getSession().getAccessToken();
                    String nom = response.getName();
                    String prenom = response.getFirstName();
                    delegate.onFacebookConnected(response.getEmail(), response.getId(), session, nom, prenom);
                }
            });
        }

        @Override
        public void onNotAcceptingPermissions(Permission.Type type) {
            // user didn't accept READ or WRITE permission
            Log.w("FB", String.format("You didn't accept %s permissions", type.name()));
        }

        @Override
        public void onThinking() {
            Log.w("FB", "thinking");
        }

        @Override
        public void onException(Throwable throwable) {
            Log.w("FB", throwable.toString());
        }

        @Override
        public void onFail(String reason) {
            Log.w("FB", reason);
        }

    /*
     * You can override other methods here:
     * onThinking(), onFail(String reason), onException(Throwable throwable)
     */
    };


    public void onActivityResult(FragmentActivity activity, int requestCode, int resultCode, Intent data) {
        mSimpleFacebook.onActivityResult(activity, requestCode, resultCode, data);
    }

    public void connect() {
        mSimpleFacebook.login(onLoginListener);
    }

    public void inviteFriends() {
        mSimpleFacebook.invite("I invite you to use this app", new OnInviteListener() {
            @Override
            public void onComplete(List<String> invitedFriends, String requestId) {
                Log.e("Test", "test");
                Log.e("List amis", invitedFriends.get(0));
            }

            @Override
            public void onCancel() {
                Log.e("Cancel", "test");

            }

            @Override
            public void onException(Throwable throwable) {
                Log.w("FB", throwable.toString());
            }

            @Override
            public void onFail(String reason) {
                Log.w("FB", reason);
            }
        }, "test");
    }

}
